<?php


namespace App\Http\Repositories\UserCenter;

use App\Models\UserModels\Models\UserRoleModel as RoleModel;
use App\Http\Repositories\BaseRepository as Repository;

class RolesRepository extends Repository
{
    private $role;
    public function __construct(RoleModel $role)
    {
        $this->role = $role;
    }

    public function getRolesList($data)
    {
        $where = [];

        if ($data['operate'] == 1) {
            is_real_exists(@$data['role']) && $where['role'] = ['like', '%'.$data['role'].'%'];
            is_real_exists(@$data['status']) && $where['status'] = $data['status'];
            return $this->role->getListByPage($where);
        }

        $res = $this->role->getList($where)->toArray();

        return $this->generateTree($res);
    }

    public function getRolesInfo($data)
    {
        $where['id'] = $data['id'];

        return $this->role->getInfo($where);
    }

    /**
     * 添加
     * @param $data
     * @return mixed
     */
    public function addRoles($data)
    {
        $data = $this->role->setUserInfo($data);

        return $this->role->addItem($data);
    }

    /**
     * 删除
     * @param $data
     * @return array
     */
    public function delRoles($data)
    {
        return $this->role->deleteItem($data);
    }

    /**
     * @param $data
     * @return array
     * @throws \App\Exceptions\ApiException
     */
    public function editRoles($data)
    {
        $where['id'] = $data['id'];

        $this->role->editItem($where, $data);

        return [];
    }

    public function getRolesTreeList($data)
    {
        $list = $this->role
            ->get()
            ->toArray();

        return $this->generateTreeStr($list, 'role');
    }
}
